package com.raulraja.model.domain;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.ws.rs.Path;
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Path("/person/")
@XmlRootElement
@XmlType(name = "person", namespace = PersistentEntity.NAMESPACE)
public class Person extends PersistentEntity<Person> {

	@Id
	private Long id;

	private String name;

	private String email;

	@ManyToOne
	private Person parent;

	@OneToMany(mappedBy = "parent")
	private List<Person> children = new ArrayList<Person>();

	@XmlElement(type = Long.class)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@XmlElement(type = String.class)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@XmlElement(type = String.class)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@XmlElement(type = Person.class)
	public Person getParent() {
		return parent;
	}

	public void setParent(Person parent) {
		this.parent = parent;
	}

	@XmlElement(type = Person.class, name = "person", namespace = PersistentEntity.NAMESPACE)
	@XmlElementWrapper(name = "children")
	public List<Person> getChildren() {
		return children;
	}

	public void setChildren(List<Person> children) {
		this.children = children;
	}

	public Class<Person> getMappedClass() {
		return Person.class;
	}

	public Person updateFromRequest(Person person) {
		setName(person.getName());
		setEmail(person.getEmail());
		return this;
	}


}
